#!/bin/bash
# Author: fanyongfeng
# 安装 kafka-eagle
# docs: https://docs.kafka-eagle.org/2.installation

######################################################
# kafka-eagle不支持访问kraft协议的kafka集群，推荐使用kafka-ui
######################################################

# 使用root用户执行
[[ $(id -u) -ne 0 ]] && echo "请使用root用户执行此脚本" && exit 1

# 检查java环境
[[ $JAVA_HOME == "" ]] && echo "未找到环境变量\${JAVA_HOME},请先配置java环境." && exit 1

# kafka需配置JMX
KAFKA_VERSION=2.12_3.4.1
KAFKA_EAGLE_VERSION=3.0.2
KAFKA_DIR=/data/kafka_${KAFKA_VERSION}
echo "jmxadmin readwrite" > ${KAFKA_DIR}/config/jmxremote.access
echo "jmxadmin 密码" > ${KAFKA_DIR}/config/jmxremote.password
chmod 600 ${KAFKA_DIR}/config/jmxremote.*

# 修改kafka配置文件 ${KAFKA_DIR}/bin/kafka-run-class.sh
# 1. 修改参数KAFKA_JMX_OPTS
# 2. 添加JMX_PORT
# JMX settings
if [ -z "$KAFKA_JMX_OPTS" ]; then
   KAFKA_JMX_OPTS="-Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.access.file=$base_dir/config/jmxremote.access -Dcom.sun.management.jmxremote.password.file=$base_dir/config/jmxremote.password -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=本机内网IP地址 -Djava.net.preferIPv4Stack=true"
fi

# JMX port to use
JMX_PORT=9999

################################ 安装kafka-eagle ################################

# 下载安装包
[[ -f kafka-eagle-bin-${KAFKA_EAGLE_VERSION}.tar.gz ]] || wget -4 https://codeload.github.com/smartloli/kafka-eagle-bin/tar.gz/refs/tags/v${KAFKA_EAGLE_VERSION}
if [[ $? -ne 0 ]]
then
    echo "kafka-eagle-bin-3.0.2.tar.gz下载失败，请重试或手动上传此安装包"
    exit 1
fi

tar zxf kafka-eagle-bin-${KAFKA_EAGLE_VERSION}.tar.gz
cd kafka-eagle-bin-${KAFKA_EAGLE_VERSION} && tar zxf efak-web-${KAFKA_EAGLE_VERSION}-bin.tar.gz
mkdir /data
mv efak-web-${KAFKA_EAGLE_VERSION} /data/kafka-eagle && rm -rf /data/kafka-eagle-bin-${KAFKA_EAGLE_VERSION}
 
# 修改配置文件/data/kafka-eagle/conf/system-config.properties
zk_addr="zk1:2181,zk2:2181,zk3:2181"
 
sed -i 's/,cluster2//g'  /data/kafka-eagle/conf/system-config.properties
sed -i "s/cluster1.zk.list.*/cluster1.zk.list=${zk_addr}/g" /data/kafka-eagle/conf/system-config.properties
sed -i 's/cluster2/#cluster2/g' /data/kafka-eagle/conf/system-config.properties
sed -i 's/kafka.zk.limit.size.*/kafka.zk.limit.size=3/g' /data/kafka-eagle/conf/system-config.properties
sed -i 's/hadoop/data/g' /data/kafka-eagle/conf/system-config.properties
 
# 注释mysql，启用sqlite
# 手动更改配置文件
 
# 添加环境变量
echo 'export KE_HOME=/data/kafka-eagle' > /etc/profile.d/kafka-eagle.sh
echo 'export PATH=$KE_HOME/bin:$PATH' >> /etc/profile.d/kafka-eagle.sh
source /etc/profile
 
# 启动kafka-eagle
cd /data/kafka-eagle
./bin/ke.sh start
# 访问地址： http://IP:8048/
# 用户名:admin 密码:123456，登录后可在web控制台进行密码修改。
 
# 如果忘记密码，可直接在数据库中查看
sqlite3 /data/kafka-eagle/db/ke.db <<EOF
select * from ke_users;
-- 修改密码
update ke_users set password='密码' where username='admin';
.quit
EOF

# sqlite开启wal模式，解决数据库文件被锁问题
sqlite3 /data/kafka-eagle/db/ke.db <<EOF
PRAGMA journal_mode = WAL;
PRAGMA wal_autocheckpoint=1000;
PRAGMA SYNCHRONOUS=NORMAL;
pragma journal_size_limit=409600;
.quit
EOF
 
# 开机启动
echo "/data/kafka-eagle/bin/ke.sh start" >> /etc/rc.local
chmod a+x /etc/rc.d/rc.local
 
# 定期删除日志
echo "0 1 * * * find /data/kafka-eagle/logs -type f -mtime +1 -name '*.log.20*' | xargs -r -n 10 rm -f" >> /var/spool/cron/root
echo "0 1 * * * truncate -s 0 /data/kafka-eagle/logs/ke_console.out"  >> /var/spool/cron/root